var states = Class.create();
Object.extend(states, {
	add_state: function(form_id){
		var check = this.state_submit_check(form_id); 
		if(check){
			this.add_state_ajax(form_id);
			$(form_id).disable();
			return false;
		} else {
			return false;
		}
	},
	add_state_ajax:function(form_id){ 
		var check = this.state_submit_check(form_id);
		var form_obj=$(form_id);
		var pars=form_obj.serialize();
		var request_url ='/~comment.signature.add_state';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters : pars,
				onComplete : function (transport){

					window.location.href='/~comment.signature.index';

				}.bindAsEventListener(this)
			});
		form_obj.enable();
	},
	refush_page: function(url){
		window.location.href=url;
	},
	state_submit_check:function(form_id){
		var form_obj = $(form_id);
		
		if(($('state').value == '请填写您的心情!')&& ($('smile_tage').value == '')) {  return false;  }
		
		if(($('state').value == '我正在做什么。。。')&& ($('smile_tage').value == '')) { return false;  }
		
		if(($('state').value == '') && ($('smile_tage').value == ''))   { return false; }
		
		if(form_obj.state.value.length>100){  			 
			return false;	
		}else{
			if(($('state').value == '请填写您的心情!')|| $('state').value == '我正在做什么。。。' )
			$('state').value = '';
			return true;
		}
	},
	remove_state: function(id){
		if(!confirm('您确认要删除此条纪录吗？')){   return false; }
		var request_url = '/~comment.signature.delete_state';
		var myAjax = new Ajax.Request(request_url,{
			method : 'post',
			parameters :'quest_id='+id, 
			asynchronous: true,
			onComplete : function (transport){
					window.location.href='/~comment.signature.index';
	
				 }
		});
	},	
	show_reply:function(div_id){
		$(div_id).style.display = 'block';
	},
	hidden_reply:function(div_id){
		$(div_id).style.display = 'none';
	},	
	add_state_index: function(form_id){	
		var check = this.state_submit_check(form_id);
		if(check){ 
			var form_obj=$(form_id);
			var pars=form_obj.serialize();
			var request_url ='/~comment.signature.add_state';
			var myAjax = new Ajax.Request(request_url,{
					method : 'post',
					parameters : pars,
					onComplete : function (transport){
	//					 window.location.reload();
						 	window.location.href='/~index.index.index';
					}.bindAsEventListener(this)
				});
			$(form_id).enable;
			return false;
		} else {
		   return false;
		}
	},
	change_state_div:function(id){
		$(id).style.display = 'none';
		$('state_new').style.display = 'block';
	},
	show_state_smile:function(id){
		$(id).style.dispaly = 'block';
	},
	state_face:function(id,face){
		var img_url = '/res/comment/images/smiles2/em'+id+'.gif';
		$('state_button').style.background = "url("+img_url+") no-repeat";
		$('state_button').style.height = '19px';
		if(id==1){  $('smile_tage').value=''; $('state_button').style.background="url() no-repeat";}
		else {  $('smile_tage').value=face; }

	},
	MaxTextareaLen : 100,
	last : 100,
	TextCountLen : new Array(),
	countTextShow : function (textareaName,spanName) {	//alert(textareaName);	//显示文本区域字数函数
		if($(textareaName).value.length <= this.MaxTextareaLen){
			this.last = this.MaxTextareaLen - $(textareaName).value.length;
			this.TextCountLen[this.MaxTextareaLen] = $(textareaName).value;
//			$(spanName).innerHTML=" 已输入" + $(textareaName).value.length +"个字；剩余" + this.last + "个字。";
			//$(spanName).innerHTML=$(textareaName).value.length+'/100';
			$(spanName).style.display = 'block';
			$(spanName).innerHTML="你还可以输入" + this.last + "个字";
		}else{
			this.last = 0;
			this.TextCountLen[this.MaxTextareaLen] = $(textareaName).value.substr(0,this.MaxTextareaLen);
			$(textareaName).value = this.TextCountLen[this.MaxTextareaLen];
			$(textareaName).focus();
		}
	},
	change_index_div:function(div_id,div_id2){
		$(div_id).style.display = 'none';
		$(div_id2).style.display = 'block';
	},
	onfocus_signature_text:function(id){
		var state_value = $(id).value ;
		if(state_value == '我正在做什么。。。'){
			$(id).value = '';
		}
	},
	onblur_signature_text:function (id){
		var state_value = $(id).value ;
		if(state_value == ''){
			$(id).value = '我正在做什么。。。';
		}
	},
	display_tip:function(id){
		$(id).style.display = 'none';
		this.onblur_signature_text('state');
	}
});

var smiles = Class.create();
Object.extend(smiles, {
	hide:false,
	show_smile:function(id){
			$(id).blur();
			if(smiles.hide){
				smiles.hide = false;
				$(id).style.display = 'none';
			}else{
				smiles.hide = true;
				$(id).style.display = 'block';
				$(id).focus();
			}
		},
	chick_face:function(id,tag){
		var connent = $('content_'+id).value;
		    connent = connent+tag;
		    $('content_'+id).value = connent;
			$('smile_'+id).style.display = 'none';
	},
	hide_smile:function(id){ 
			//smiles.hide = false;
			$(id).style.display = 'none';
		//}

	},
	show_smile_index:function(event,id){
			event = $E(event);
			var src = Event.element(event);
			var dem = src.getDimensions();
			var offset = src.cumulativeOffset();
			var t = new Template(Gift.template);
			$('smile').absolutize();
			$('smile').setStyle({
				top: dem.height - 20 + 'px',
				left: 50 + 'px'
			});
		//	$(id).style.display='none';

	}

});

var SmilePane2 = Class.create();
Object.extend(SmilePane2.prototype, {
	id: '',
	chooseFunc: null,
	trigger: null,
	initialize: function(id, trigger, chooseFunc){
		this.id = id;
		this.chooseFunc = chooseFunc;
		this.trigger = $(trigger);

	},
	bindHandlers: function(){
		$('smile_'+this.id).observe('click', this.chooseSmile.bindAsEventListener(this));
		Event.observe(document.body, 'click', this.cancelPanel.bindAsEventListener(this));
	},	
	show: function(event, adjust){
		event = $E(event);
		var src = Event.element(event);
		
		if($('smile_'+this.id) && $('smile_'+this.id).getStyle('display')!='none'){
			$('smile_'+this.id).hide();
			return;
		}

		if(!$('smile_'+this.id)){
			var text = this.genHTML();
			$(document.body).insert({bottom: text});
			this.bindHandlers();
		}

		var offset = src.cumulativeOffset();
		var dem = src.getDimensions();
		var left = offset.left;
		var top = offset.top + dem.height ;
		if(adjust){
			left += adjust.left;
			top += adjust.top;
		}

		$('smile_' + this.id).absolutize ();
		$('smile_' + this.id).setStyle({
			left: left + 'px',
			top: top + 9 + 'px'
		});

		$('smile_' + this.id).show();
	},
	genHTML: function(){
		var text = '<div id="smile_'+this.id+'" style="outline:none;z-index:99">\
		<div class="smile-wrap">\
		<div  class="simgs"  >';

		for(var i=1; i<=64; i++){
			text += '<img style="width: 21px; height: 21px;" src="/res/comment/images/smiles2/em'+i+'.gif" index="'+i+'" class="smile"/>';
		}

		text += "</div></div></div>";

		return text;
	},
	cancelPanel: function(event){
		event = $E(event);
		var src = Event.element(event);
		if(src.descendantOf('smile_'+this.id) || src==$('smile_'+this.id) || src.id==this.trigger.id){
		}else{
			$('smile_'+this.id).hide();
		}
	},
	chooseSmile: function(event){
		event = $E(event);
		var src = Event.element(event);
		if(src.hasClassName('smile')){
			var index = src.getAttribute('index');
			this.chooseFunc.call(null, index);
			$('smile_'+this.id).hide();
		}
	}
});
